﻿using EntitySpaces.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using yncTech.Common;

public partial class Admin_Fin_Fin007c : AdminBasePage
{

    decimal lendID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[VLendBoxMetadata.ColumnNames.LendID] != null)
        {
            lendID = decimal.Parse(Request.QueryString[VLendBoxMetadata.ColumnNames.LendID]);
        }


        if (!IsPostBack)
        {
            FinLend fs = new FinLend();
            fs.LoadByPrimaryKey(lendID);

            //if (fs.LendStatus != (int)LendStatus.Pending)
            //{
            //    btnRemove.Visible = false;
            //}
        }

    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        GridDataBind(e.NewPageIndex);
    }

    void GridDataBind(int page)
    {

        FinLendReceiptQuery q1 = new FinLendReceiptQuery("q1");
        FinReceiptQuery q2 = new FinReceiptQuery("q2");
        FinBoxReceiptsQuery q3 = new FinBoxReceiptsQuery("q3");
        FinBoxQuery q4 = new FinBoxQuery("q4");
        TSCodeTableQuery q5 = new TSCodeTableQuery("q5");

        q1.InnerJoin(q2).On(q1.ReceiptID == q2.ReceiptID);
        q1.InnerJoin(q3).On(q2.ReceiptID == q3.ReceiptID);
        q1.InnerJoin(q4).On(q3.BoxID == q4.BoxID);
        q1.InnerJoin(q5).On(q2.DocumentsType == q5.Code);

        q1.Select(q4.BoxNo, q2.DocumentsNo, q2.DocumentsType, q2.Reference, q2.ReferenceKey, q5.Value1.As("DocTypeName"), q3.ReceiptID, q4.BoxID);
        q1.Where(q1.LendID == lendID);

        pageGrid.DataSource = q1.LoadDataTable();
        pageGrid.CurrentPage = page;
        pageGrid.DataBind();
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";

            List<decimal> boxIDs = new List<decimal>();
            List<decimal> repList = new List<decimal>();
            using (esTransactionScope tr = new esTransactionScope())
            {
                foreach (RepeaterItem item in pageGrid.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("chk_sel");
                    if (chkBox.Checked)
                    {
                        Label lbBox = (Label)item.FindControl("lbBoxID");
                        boxIDs.Add(decimal.Parse(lbBox.Text));
                        Label lbRep = (Label)item.FindControl("lbReptID");
                        repList.Add(decimal.Parse(lbRep.Text));
                    }
                }

                if (boxIDs.Count > 0)
                {
                    FinLendReceiptCollection lendReps = new FinLendReceiptCollection();
                    lendReps.Query.Where(lendReps.Query.ReceiptID.In(repList.ToArray()) && lendReps.Query.LendID == lendID);
                    lendReps.Query.Load();
                    lendReps.MarkAllAsDeleted();
                    lendReps.Save();

                    FinReceiptCollection reps = new FinReceiptCollection();
                    reps.Query.Where(reps.Query.ReceiptID.In(repList.ToArray()));
                    reps.Query.Load();

                    foreach (var item in reps)
                    {
                        item.StorageStatus = ConstantValue.StorageStatusCode.InStock;
                        item.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                        item.UpdatedDttm = DateTime.Now;
                    }
                    reps.Save();

                    ///凭证移除后检查凭证盒中是否还有外借的凭证，如没有，则将状态改为在库
                    FinLendReceiptQuery q1 = new FinLendReceiptQuery("q1");
                    FinBoxReceiptsQuery q2 = new FinBoxReceiptsQuery("q2");

                    q1.InnerJoin(q2).On(q1.ReceiptID == q2.ReceiptID);
                    q1.Where(q2.BoxID.In(boxIDs.ToArray()));
                    q1.Select(q2.BoxID);
                    var boxs = q1.LoadDataTable();

                    List<decimal> tempList = new List<decimal>();
                    for (int i = 0; i < boxs.Rows.Count; i++)
                    {
                        decimal tmpID = (decimal)boxs.Rows[i][0];
                        if (!boxIDs.Exists(r => r == tmpID))
                        {
                            tempList.Add(tmpID);
                        }
                    }

                    if (tempList.Count > 0)
                    {
                        FinBoxCollection boxCollection = new FinBoxCollection();
                        boxCollection.Query.Where(boxCollection.Query.BoxID.In(tempList.ToArray()));
                        boxCollection.Query.Load();

                        foreach (var box in boxCollection)
                        {
                            box.StorageStatus = ConstantValue.StorageStatusCode.InStock;
                            box.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                            box.UpdatedDttm = DateTime.Now;
                        }
                        boxCollection.Save();
                    }



                }

                tr.Complete();

                GridDataBind(1);
                WebHelper.CloseDialog(true);
            }

        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }
}